<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="流水号" prop="serialNo">
        <el-input
          v-model="queryParams.serialNo"
          placeholder="请输入流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完工单号" prop="productFinishNo">
        <el-input
          v-model="queryParams.productFinishNo"
          placeholder="请输入完工单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产计划编号" prop="productPlanId">
        <el-input
          v-model="queryParams.productPlanId"
          placeholder="请输入生产计划编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产作业编号" prop="productJobId">
        <el-input
          v-model="queryParams.productJobId"
          placeholder="请输入生产作业编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料档案编号" prop="materialId">
        <el-select v-model="queryParams.materialId" placeholder="请选择物料档案编号" clearable filterable size="small">
          <el-option
            v-for="dict in materialIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检验批次" prop="materialBatchNo">
        <el-input
          v-model="queryParams.materialBatchNo"
          placeholder="请输入检验批次"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="productDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.productDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择生产日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检验标准编号" prop="checkStandardId">
        <el-input
          v-model="queryParams.checkStandardId"
          placeholder="请输入检验标准编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验结果" prop="checkResult">
        <el-select v-model="queryParams.checkResult" placeholder="请选择检验结果" clearable filterable size="small">
          <el-option
            v-for="dict in checkResultOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检验人" prop="checkUser">
        <el-input
          v-model="queryParams.checkUser"
          placeholder="请输入检验人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验时间" prop="checkDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.checkDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择检验时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:productCheck:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:productCheck:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:productCheck:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:productCheck:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="流水号" align="center" prop="serialNo" show-overflow-tooltip/>
      <el-table-column label="计划编号" align="center" prop="productPlanId" show-overflow-tooltip/>
      <el-table-column label="完工单号" align="center" prop="productFinishNo" show-overflow-tooltip/>
      <el-table-column label="作业名称" align="center" prop="productJobId" :formatter="productJobIdFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="产线名称" align="center" prop="productLineId" :formatter="productLineIdFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="检验批次" align="center" prop="materialBatchNo" show-overflow-tooltip/>
      <el-table-column label="产品名称" align="center" prop="materialId" :formatter="materialIdFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="生产日期" align="center" prop="productDate" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检验项" align="center" prop="checkStandardId" :formatter="checkStandardIdFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="生产数量" align="center" prop="productQuantity" show-overflow-tooltip/>
      <el-table-column label="抽检数量" align="center" prop="checkQuantity" show-overflow-tooltip/>
      <el-table-column label="检验值" align="center" prop="checkValue" show-overflow-tooltip/>
      <el-table-column label="阈值上限" align="center" prop="maxValue" show-overflow-tooltip/>
      <el-table-column label="阈值下限" align="center" prop="minValue" show-overflow-tooltip/>
      <el-table-column label="检验结果" align="center" prop="checkResult" :formatter="checkResultFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="检验人" align="center" prop="checkUser" :formatter="checkUserFormat"
                       show-overflow-tooltip
      />
      <el-table-column label="检验时间" align="center" prop="checkDate" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:productCheck:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:productCheck:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品检验对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="完工单号" prop="productFinishNo">
          <el-input disabled v-model="form.productFinishNo" placeholder="请选择完工单号">
            <el-button slot="append" @click="handleProductFinish">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="检验标准" prop="checkStandardId">
          <el-input disabled v-model="form.checkStandardId" placeholder="请选择检验标准编号">
            <el-button slot="append" @click="handleCheckStandard">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="抽检数量" prop="checkQuantity">
          <el-input v-model="form.checkQuantity" placeholder="请输入抽检数量"/>
        </el-form-item>
        <el-form-item label="检验值" prop="checkValue">
          <el-input v-model="form.checkValue" placeholder="请输入检验值"/>
        </el-form-item>
        <el-form-item label="检验结果" prop="checkResult">
          <el-select v-model="form.checkResult" placeholder="请选择检验结果" clearable filterable>
            <el-option
              v-for="dict in checkResultOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <ProductFinishSelect @onSelected="onSelectedProductFinish" @onCancel="onCancelProductFinish"
                         :open="productFinishOpen"
    ></ProductFinishSelect>

    <StandardSelect @onStandardSelected="onSelectedCheckStandard" @onStandardCancel="onCancelCheckStandard"
                    :open="checkStandardOpen"
    ></StandardSelect>
  </div>
</template>

<script>
import {
  addProductCheck,
  delProductCheck,
  exportProductCheck,
  getProductCheck,
  listProductCheck,
  updateProductCheck
} from '@/api/mes/productCheck'
import { parseTime } from '../../../utils/neu'
import ProductFinishSelect from '@/components/product/ProductFinishSelect.vue'
import StandardSelect from '@/components/MesCheck/StandardSelect.vue'

export default {
  name: 'ProductCheck',
  components: {
    StandardSelect,
    ProductFinishSelect
  },
  data() {
    return {
      productFinishOpen: false,
      checkStandardOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品检验表格数据
      productCheckList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 生产线编号字典
      productLineIdOptions: [],
      // 生产作业编号字典
      productJobIdOptions: [],
      // 物料档案编号字典
      materialIdOptions: [],
      // 检验标准编号字典
      checkStandardIdOptions: [],
      // 检验结果字典
      checkResultOptions: [],
      // 检验人字典
      checkUserOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNo: null,
        productFinishNo: null,
        productPlanId: null,
        productJobId: null,
        materialId: null,
        materialBatchNo: null,
        productDate: null,
        checkStandardId: null,
        checkResult: null,
        checkUser: null,
        checkDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productFinishNo: [
          { required: true, message: '完工单号不能为空', trigger: 'blur' }
        ],
        checkStandardId: [
          { required: true, message: '检验标准编号不能为空', trigger: 'blur' }
        ],
        checkQuantity: [
          { required: true, message: '抽检数量不能为空', trigger: 'blur' }
        ],
        checkValue: [
          { required: true, message: '检验值不能为空', trigger: 'blur' }
        ],
        checkResult: [
          { required: true, message: '检验结果不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getQueryData('query_product_line').then(res => {
      this.productLineIdOptions = res.data
    })
    this.getQueryData('query_product_job').then(res => {
      this.productJobIdOptions = res.data
    })
    this.getQueryData('query_material').then(res => {
      this.materialIdOptions = res.data
    })
    this.getQueryData('query_check_standard').then(res => {
      this.checkStandardIdOptions = res.data
    })
    this.getDicts('check_result').then(response => {
      this.checkResultOptions = response.data
    })
    this.getQueryData('query_user').then(res => {
      this.checkUserOptions = res.data
    })
  },
  methods: {
    // 生产完工单对话框
    handleProductFinish() {
      this.productFinishOpen = true
    },
    onSelectedProductFinish(row) {
      this.$set(this.form, 'productFinishNo', row.batchNo)
      this.productFinishOpen = false
    },
    onCancelProductFinish() {
      this.productFinishOpen = false
    },
    // 检验标准对话框
    handleCheckStandard() {
      this.checkStandardOpen = true
    },
    onSelectedCheckStandard(row) {
      const checkStandard = row[0]
      this.$set(this.form, 'checkStandardId', checkStandard.id)
      this.checkStandardOpen = false
    },
    onCancelCheckStandard() {
      this.checkStandardOpen = false
    },
    parseTime,
    /** 查询产品检验列表 */
    getList() {
      this.loading = true
      listProductCheck(this.queryParams).then(response => {
        this.productCheckList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 生产线编号字典翻译
    productLineIdFormat(row, column) {
      return this.selectDictLabel(this.productLineIdOptions, row.productLineId)
    },
    // 生产作业编号字典翻译
    productJobIdFormat(row, column) {
      return this.selectDictLabel(this.productJobIdOptions, row.productJobId)
    },
    // 物料档案编号字典翻译
    materialIdFormat(row, column) {
      return this.selectDictLabel(this.materialIdOptions, row.materialId)
    },
    // 检验标准编号字典翻译
    checkStandardIdFormat(row, column) {
      return this.selectDictLabel(this.checkStandardIdOptions, row.checkStandardId)
    },
    // 检验结果字典翻译
    checkResultFormat(row, column) {
      return this.selectDictLabel(this.checkResultOptions, row.checkResult)
    },
    // 检验人字典翻译
    checkUserFormat(row, column) {
      return this.selectDictLabel(this.checkUserOptions, row.checkUser)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNo: null,
        productFinishId: null,
        productFinishNo: null,
        productPlanId: null,
        productScheduleId: null,
        productLineId: null,
        productJobId: null,
        materialId: null,
        materialBatchNo: null,
        productDate: null,
        productQuantity: null,
        checkStandardId: null,
        checkQuantity: null,
        checkValue: null,
        maxValue: null,
        minValue: null,
        checkResult: null,
        remark: null,
        checkUser: null,
        checkDate: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加产品检验'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProductCheck(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改产品检验'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductCheck(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addProductCheck(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除产品检验编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delProductCheck(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有产品检验数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportProductCheck(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
